<template>
    <div class="app-container">
        <el-button type="primary" @click="createPrivateKey">创建私钥</el-button>
        <div>
            {{createPrivateKey_privateKey}}
        </div>
        <div>
            {{createPrivateKey_publicKey}}
        </div>

        
        <div>
            <el-input style="width:150px" placeholder="请输入a-z1-5,12位以内的账户名" id="createAccount-accountName">
            </el-input>
            <el-button type="primary" @click="createAccount" style="marginTop:10px">创建账户</el-button>
            <div>
                {{createAccount_result}}
            </div>
        </div>
        <div>
            <el-input style="width:150px" placeholder="请输入账户名" id="deployContract-accountName">
            </el-input>
            <el-input style="width:150px" placeholder="请输入合约名字" id="deployContract-contractName">
            </el-input>
            <el-button type="primary" @click="deployContract" style="marginTop:10px">部署合约</el-button>
        </div>
        <div>
            <el-input style="width:150px" placeholder="请输入账户名" id="loadContract-accountName">
            </el-input>
            <el-input style="width:150px" placeholder="请输入合约名字" id="loadContract-contractName">
            </el-input>
            <el-button type="primary" @click="loadContract" style="marginTop:10px">加载合约</el-button>
        </div>
        <div>
            <el-input style="width:150px" placeholder="请输入想创建的代币的名称" id="createCurrency-currencyName">
            </el-input>
            <el-button type="primary" @click="createCurrency" style="marginTop:10px">创建代币</el-button>
        </div>
    </div>
    
    
</template>

<script>
const fs =  require('fs');
const Eos = require('eosjs')
const ecc = require('eosjs-ecc')
const binaryen = require('binaryen')
const stake_cpu_quantity = "1 SYS";//给用户抵押的数量-用于cpu使用
const stake_net_quantity = "1 SYS";//给用户抵押的数量-用于网络使用
const pk = '5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3';
const eosServer = 'http://127.0.0.1:8888';
const publicKey = 'EOS54BTnbboRETLfqZt7zgdmdU4GLpyLXhJ33H83BwNA7UduLLS26';
const eos = Eos({
    binaryen: binaryen,
    keyProvider: [pk],
    chainId: '706a7ddd808de9fc2b8879904f3b392256c83104c1d544b38302cc07d9fca477',
    httpEndpoint: eosServer
})

export default{

    data() {
        return {
            createPrivateKey_privateKey: '',
            createPrivateKey_publicKey: '',
            createAccount_result: ''
            
        }
    },
    methods: {
        createPrivateKey(){
            this.$message('创建私钥');
            let privateKey = ecc.seedPrivate('dafei');
            let publicKey = ecc.privateToPublic(privateKey);
            this.createPrivateKey_privateKey = privateKey
            this.createPrivateKey_publicKey = publicKey
        },
        createAccount(){
            console.log('publicKey-->',publicKey)
            this.$message('创建账户');
            var account = document.getElementById("createAccount-accountName").value;
            eos.transaction(tr => {
                tr.newaccount({
                    creator: 'eosio',
                    name: account,
                    owner: publicKey,
                    active: publicKey
                })
                tr.buyrambytes({
                    payer: 'eosio',
                    receiver: account,
                    bytes: 8192
                })
                tr.delegatebw({
                    from: 'eosio',
                    receiver: account,
                    stake_net_quantity: stake_net_quantity,
                    stake_cpu_quantity: stake_cpu_quantity,
                    transfer: 0
                })
            }).then(result => {
                if (result) {
                    this.createAccount_result = { code: 0, msg: "success", result }
                } else {
                    this.createAccount_result = { code: 500, msg: result, data: {} }
                }
            }).catch(e => {
                this.createAccount_result = { code: 500, msg: e, data: {} }
            });
        },
        deployContract(){

            var contract = document.getElementById("deployContract-contractName").value;
            var account = document.getElementById("deployContract-accountName").value;
            const wasm = fs.readFileSync(`contracts/${contract}/${contract}.wasm`)
            const abi = fs.readFileSync(`contracts/${contract}/${contract}.abi`)

            eos.setcode(account, 0, 0, wasm)
            eos.setabi(account, JSON.parse(abi))

            const code = eos.getCode(account)
            console.log('打印code....')
            console.log(code)
        },
        loadContract(){
            // this.$message('加载合约');
            var inputValue = document.getElementById("loadContract-contractName").value;
            this.$message(inputValue);
        },
        createCurrency(){
            
            this.$message('创建代币');
            // eos.transaction('hiblock.token', myaccount => {
            //     // Create the initial token with its max supply
            //     // const options = {authorization: 'dafei'} // default
            //     myaccount.create('dafei', '1000000000.0000 HB@dafei')//\, options)
            //     // Issue some of the max supply for circulation into an arbitrary account
            //     // myaccount.issue('dafei', '10000.0000 HB', 'issue')
            // })

            // const balance = eos.getCurrencyBalance('lxh', 'lxh', 'HB')
            // console.log('Currency Balance', balance)

            eos.transaction({
                actions: [{
                    account: 'dafei',
                    name: 'create',
                    authorization: [{
                        actor: 'dafei',
                        permission: 'active'
                    }],
                    data: {
                        issuer: 'dafei',
                        maximum_supply: '1000.0000 HB'
                    }
                }]
            })
        }
    }
}
</script>
